<template>
	<div class="main-container">
		<h1>选择问题</h1>
		<!-- 输入区域 -->
		<div class="input-section">
			<div class="options-list">
				<el-select v-model="selectedQuestion" placeholder="请选择问题" class="full-width" @change="getSelect" loading-text="加载中..."
				 :loading="loading">
					<el-option v-for="item in ListData" :key="item.tid" :label="item.title" :value="item.tid">
					</el-option>
				</el-select>

				
			</div>

			<el-button type="primary" icon="el-icon-refresh" class="shuffle-btn" @click="generateResult()" >
				帮我选择！
			</el-button>
		</div>


	</div>
</template>

<script>
	import Cookies from "js-cookie";
	export default {
		name: "Select",
		data() {
			return {
				ListData: [],
				selectedQuestion: null, // 新增选择器绑定值
				loading: false, // 新增加载状态
				total: "", //存储选项个数
				result: ""
			}
		},
		mounted() {
			this.getQuestion();
		},
		methods: {
			getQuestion() {
				this.loading = true;
				let _this = this;
				const loginName = Cookies.get("loginName");
				this.$axios
					.get("/question/list", {
						params: {
							loginName: loginName
						}
					})
					.then(function(response) {
						_this.ListData = response.data; // 存储全部数据
						_this.total = _this.ListData.length;
						_this.loading = false;
						// console.log('接口返回数据：', response.data); // 查看实际数据结构
					})
					.catch(console.error);

			},
			getSelect(selectedTid) {
				if (!selectedTid) return;
				// 查找完整问题对象
				const selectedQuestion = this.ListData.find(
					item => item.tid === selectedTid
				);

				
			},
			generateResult() {
				const loginName2=Cookies.get("loginName");
				 if (!this.selectedQuestion) {
				    this.$message.warning('请先选择问题');
				    return;
				  }
				let _this = this;
				const stid = this.selectedQuestion;
				
				this.$axios
					.get("/option/select/" + stid,{
						params:{
							loginName:loginName2
							}
					})
					.then(function(response) {
						_this.result = response.data.content;
						// 结果展示逻辑
						_this.$alert(_this.result, '选择结果', {
							confirmButtonText: '确定'
						});

					})
					.catch(console.error);
			}
	

		}
	}
</script>

<style>

</style>
